package cn.neusoft.baibang.web.controller;

import cn.neusoft.baibang.service.IProductTypeService;
import cn.neusoft.baibang.domain.ProductType;
import cn.neusoft.baibang.query.ProductTypeQuery;
import cn.neusoft.baibang.result.JSONResult;
import cn.neusoft.baibang.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/productType")
public class ProductTypeController {

    @Autowired
    public IProductTypeService productTypeService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody ProductType productType){
        if(productType.getId()!=null){
                productTypeService.updateById(productType);
        }else{
                productTypeService.insert(productType);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
            productTypeService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(productTypeService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(productTypeService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody ProductTypeQuery query){
        Page<ProductType> page = new Page<ProductType>(query.getPage(),query.getRows());
        page = productTypeService.selectPage(page);
        return JSONResult.success(new PageList<ProductType>(page.getTotal(),page.getRecords()));
    }

    /**
     * 获取无限级数
     */
    @RequestMapping(value = "/treeData",method = RequestMethod.GET)
    public JSONResult getTreeDate(){
        List<ProductType> treeData =  productTypeService.getTreeDate1();
        return JSONResult.success(treeData);
    }
}
